<?php

namespace app\controller;


use app\BaseController;
use think\facade\View;
use think\facade\Config;
use Illuminate\View\Compilers\BladeCompiler;

class Template extends BaseController
{

    // 暂时没有使用，直接在路由中使用

    // 展示模板
    public function show()
    {
        $module     = $this->request->route('module', 'index');
        $controller = $this->request->route('controller');
        $action     = $this->request->route('action');
        $version    = time();



        // 直接显示模板
        $template = $this->templatePath($module, $controller, $action);

        return View::fetch($template, ['version' => $version,'time' => $version]);
    }


    // 展示模板
    public function show2($module = 'index', $controller = '', $action = '')
    {
        $template = $this->templatePath($module, $controller, $action);

        return View::fetch($template);
    }

    // 模板路径
    private function templatePath($module = 'index', $controller = '', $action = '')
    {
        // 直接显示模板
        $tpl_path = 'frontend/light';
        $_path    = $this->app->getRootPath() . '/' . Config::get('view.view_dir_name') . '/';
        $_suffix  = Config::get('view.view_suffix');
        $template = $tpl_path . '/' . $module . ($controller ? '/' . $controller : '') . ($action ? '/' . $action : '');
        $file     = $_path . $template . '.' . $_suffix;
        if (!is_file($file)) {
            $template = $tpl_path . '-' . $module . ($controller ? '-' . $controller : '') . ($action ? '-' . $action : '');
            $file     = $_path . $template . '.' . $_suffix;
        }
        if (!is_file($file)) {
            die('模板文件不存在' . $file);
        }

        /*// 设置新的缓存路径
        $newPath = storage_path('framework/views/' . app()->environment());
        (new BladeCompiler())->setPath($newPath);

        // 确保目录存在
        if (!is_dir($newPath)) {
            mkdir($newPath, 0755, true);
        }*/

        return $template;
    }

}




